package com.rocksea.app.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.rocksea.app.BaseFragment;
import com.rocksea.app.R;
import com.rocksea.app.activity.BaseShowActivity;
import com.rocksea.app.adapter.PileInfoAdapter;
import com.rocksea.app.interfaces.MyExecuteCallBack;
import com.rocksea.app.tasks.QueryPileTask;
import com.rocksea.app.tasks.Temp;
import com.rocksea.app.weight.DividerListItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.InjectView;

/**
 *
 * @author 吴志华
 * @date 2017/10/26
 *
 *
 */

public class CommonBaseInfoFragment extends BaseFragment{

    public static final String MODE = "mode_key";

    @InjectView(R.id.Pile_Images)
    RecyclerView pileInfoList;
    private String baseInfoId;
    private PileInfoAdapter pileInfoAdapter;
    private int mode;

    public static CommonBaseInfoFragment newInstance(Bundle bundle) {
        CommonBaseInfoFragment fragment = new CommonBaseInfoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView()
    {
        Bundle bundle = getArguments();
        baseInfoId = bundle.getString(BaseShowActivity.BASEINFOID_KEY);
        mode = bundle.getInt(MODE,-1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        pileInfoList.setLayoutManager(layoutManager);
        pileInfoList.addItemDecoration(new DividerListItemDecoration(getContext(),DividerListItemDecoration.VERTICAL_LIST));
    }
    @Override
    protected void initData()
    {
        loading();
        if (mode!=-1&&Temp.getSonicWave()!=null)
        {
            QueryPileTask queryPileTask = new QueryPileTask();
            String[] info = queryPileTask.getSonicWaveBaseInfo(Temp.getSonicWave());
            loadCompleted();
            ArrayList<String> datas = new ArrayList<>();
            Collections.addAll(datas, info);
            pileInfoAdapter = new PileInfoAdapter(datas, getContext());
            pileInfoList.setAdapter(pileInfoAdapter);
            return;
        }
        QueryPileTask queryPileTask = new QueryPileTask(baseInfoId,callBack);
        queryPileTask.execute();
    }

    @Override
    protected void requestAgain()
    {
        initData();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.photo_fragment_layout;
    }

    private MyExecuteCallBack<String[]> callBack = new MyExecuteCallBack<String[]>() {
        @Override
        public void onError(int code, String message)
        {
            loadFailed();
            ArrayList<String> datas = new ArrayList<>();
            pileInfoAdapter = new PileInfoAdapter(datas, getContext());
            pileInfoList.setAdapter(pileInfoAdapter);
        }
        @Override
        public void onSuccess(String[] strings)
        {
            loadCompleted();
            ArrayList<String> datas = new ArrayList<>();
            Collections.addAll(datas, strings);
            pileInfoAdapter = new PileInfoAdapter(datas, getContext());
            pileInfoList.setAdapter(pileInfoAdapter);
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



}
